package org.eddy.wechar_sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.*;
class Person {

    @JSONField(name = "AGE")
    private int age;

    @JSONField(name = "FULL NAME")
    private String fullName;

    @JSONField(name = "DATE OF BIRTH")
    private Date dateOfBirth;
    public Person(){

    }

    public Person(int age, String fullName, Date dateOfBirth) {
        super();
        this.age = age;
        this.fullName = fullName;
        this.dateOfBirth = dateOfBirth;
    }




    // 标准 getters & setters
}

@Controller
@RequestMapping("/Test")
public class Testcontrol {
    @RequestMapping("/Test")
    @ResponseBody
    public JSONObject test() {

        return json();
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping("/Sql")
    @ResponseBody
    public int Sql() {
        int insert = insert();
        return insert;
    }

    @RequestMapping("/select")
    @ResponseBody
    public List<Test> select() {
        List<Test> select = sqlselect();
        return select;
    }

    public JSONObject json() {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < 2; i++) {

            jsonObject.put("AGE", 10);
            jsonObject.put("FULL NAME", "Doe " + i);
            jsonObject.put("DATE OF BIRTH", "2016/12/12 12:12:12");
            jsonArray.add(jsonObject);
        }


        List<Person> listOfPersons = new ArrayList<Person>();
        listOfPersons.add(new Person(15, "John Doe", new Date()));
        listOfPersons.add(new Person(20, "Janette Doe", new Date()));
        String jsonOutput = JSON.toJSONString(listOfPersons);
        return jsonObject;
    }

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public int insert() {
        String sql = "insert into chat_msg(send_user_id,accept_user_id,msg,sign_flag,create_time) " +
                "values(:send_user_id,:accept_user_id,:msg,:sign_flag,:create_time)";
        Map<String, Object> param = new HashMap<>();
        Date date = new Date();
        param.put("send_user_id", "1");
        param.put("accept_user_id", "1");
        param.put("msg", "1");
        param.put("sign_flag", 1);
        param.put("create_time", date);
        System.out.println(sql);
        return (int) jdbcTemplate.update(sql, param);
    }

    public List<Test> sqlselect() {
        String sql = "select * from chat_msg";
        //创建一个新的BeanPropertyRowMapper对象
        RowMapper<Test> rowMapper = new BeanPropertyRowMapper<Test>(Test.class);
        //执行静态的SQL查询，并通过RowMapper返回结果
        System.out.println("----------");
        System.out.println("----------");
        System.out.println("----------");
        System.out.println("----------");
        System.out.println("----------");
        String s = rowMapper.toString();

        List<Test> query = jdbcTemplate.query(sql, rowMapper);
        return query;

    }



}
